package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.DdRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.HaManageEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.RdFactorEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.RdFactorRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class RdFactorRepositoryImpl implements RdFactorRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveRdFactor(RdFactorEntity it) {
        jdbcTemplate.update("insert into czt_road_damage_factor(crdf_id,serial_number,category,quantized_value,create_user,create_time,del_flag,damage_factor,default_data) " +
                "values(?,?,?,?,?,?,?,?,?)", it.getCrdfId(), it.getSerialNumber(), it.getCategory(), it.getQuantizedValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(),it.getDamageFactor(),it.getDefaultData());
    }

    @Override
    public void updateRdFactor(RdFactorEntity it) {
        String sql = "update czt_road_damage_factor set serial_number= :serialNumber,category= :category,quantized_value= :quantizedValue,update_user= :updateUser,update_time= :updateTime,damage_factor= :damageFactor" +
                " where crdf_id= '" + it.getCrdfId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<RdFactorEntity> getAllRdFactor() {
        String sql = "select * from czt_road_damage_factor where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RdFactorEntity.class));
    }

    @Override
    public boolean deleteRdFactorById(String rdFactorId) {
        String sql = "update czt_road_damage_factor set del_flag = '1' where crdf_id=?";
        return jdbcTemplate.update(sql, rdFactorId) > 0;
    }

    @Override
    public void saveDdRatio(DdRatioEntity it) {
        jdbcTemplate.update("insert into czt_damage_dispersed_ratio(cddr_id,basic_intensity,dispersed_ratio,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCddrId(), it.getBasicIntensity(), it.getDispersedRatio(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateDdRatio(DdRatioEntity it) {
        String sql = "update czt_damage_dispersed_ratio set basic_intensity= :basicIntensity,dispersed_ratio= :dispersedRatio,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where cddr_id= '" + it.getCddrId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<DdRatioEntity> getAllDdRatio() {
        String sql = "select * from czt_damage_dispersed_ratio where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DdRatioEntity.class));
    }

    @Override
    public void deleteDdRatioById(String ddRatioId) {
        String sql = "update czt_damage_dispersed_ratio set del_flag = '1' where cddr_id=?";
        jdbcTemplate.update(sql, ddRatioId);
    }

    @Override
    public void deleteDdRatioByProjectId(String arrangeId) {
        String sql = "update czt_damage_dispersed_ratio set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public List<RdFactorEntity> getRdById(String crdfId) {
        String sql = "select * from czt_road_damage_factor where crdf_id='"+crdfId+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RdFactorEntity.class));
    }

    @Override
    public List<DdRatioEntity> getDdById(String cddrId) {
        String sql = "select * from czt_damage_dispersed_ratio where cddr_id='"+cddrId+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DdRatioEntity.class));
    }
}
